package com.sy.service.impl;

import com.sy.bean.Dict;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.DictMapper;
import com.sy.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl extends BaseServiceImpl<Dict, DictMapper> implements DictService {
    @Autowired
    DictMapper mapper;

    @Override
    public List<Dict> queryD(String code) {
        List<Dict> dicts = mapper.queryD(code);
        return dicts;
    }

    @Override
    public int save(Dict entity) {
        String code = mapper.queryParentCode(entity.getParentId());
        //自动生成编码
        String s = queryMaxCode(code);
        if (s == null && s.equals("")) {
            entity.setCode("1000001");
            int save = mapper.save(entity);


        } else {
            entity.setCode(Integer.parseInt(s) + 1 + "");
            //保存
            int save = mapper.save(entity);

        }

        return 1;
    }

    /**
     * 查询最大编码
     *
     * @param code
     * @return
     */
    String queryMaxCode(String code) {
        String s = mapper.queryMaxCode(code);
        return s;
    }
}
